package cn.whx.hrm.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Component
public class FeignInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
        //获取相关的属性
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.currentRequestAttributes();
        //获取当前请求
        HttpServletRequest request = requestAttributes.getRequest();
        //获取请求头中token
        String authorization = request.getHeader("Authorization");
        //将原来请求中的token放入feign发起的请求中
        template.header("Authorization",authorization);
    }
}
